﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.IO;
using System.Text;
using System.ComponentModel;

namespace VegetableStore.Util
{

    public class CipherUtil
    {
        private const string KEY = "gksksladmltkfkd";
 
        public static string Decrypt(string textToDecrypt)
        {
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
            rijndaelCipher.Mode = CipherMode.CBC;
            rijndaelCipher.Padding = PaddingMode.PKCS7;

            rijndaelCipher.KeySize = 0x80;
            rijndaelCipher.BlockSize = 0x80;
            byte[] encryptedData = Convert.FromBase64String(textToDecrypt);
            byte[] pwdBytes = Encoding.UTF8.GetBytes(KEY);
            byte[] keyBytes = new byte[0x10];
            int len = pwdBytes.Length;
            if (len > keyBytes.Length)
            {
                len = keyBytes.Length;
            }
            Array.Copy(pwdBytes, keyBytes, len);
            rijndaelCipher.Key = keyBytes;
            rijndaelCipher.IV = keyBytes;
            byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length);
            return Encoding.UTF8.GetString(plainText);
        }

        public static string Encrypt(string textToEncrypt)
        {
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
            rijndaelCipher.Mode = CipherMode.CBC;
            rijndaelCipher.Padding = PaddingMode.PKCS7;

            rijndaelCipher.KeySize = 0x80;
            rijndaelCipher.BlockSize = 0x80;
            byte[] pwdBytes = Encoding.UTF8.GetBytes(KEY);
            byte[] keyBytes = new byte[0x10];
            int len = pwdBytes.Length;
            if (len > keyBytes.Length)
            {
                len = keyBytes.Length;
            }
            Array.Copy(pwdBytes, keyBytes, len);
            rijndaelCipher.Key = keyBytes;
            rijndaelCipher.IV = keyBytes;
            ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
            byte[] plainText = Encoding.UTF8.GetBytes(textToEncrypt);
            return Convert.ToBase64String(transform.TransformFinalBlock(plainText, 0, plainText.Length));
        }

        ///// <summary>
        ///// Decrypts a string 
        ///// </summary>
        ///// <param name="encryptedString"></param>
        ///// <returns></returns>
        //public static String DecryptString(string encryptedString)
        //{
        //    if (String.IsNullOrEmpty(encryptedString)) return String.Empty;

        //    try
        //    {
        //        using (TripleDESCryptoServiceProvider cypher = new TripleDESCryptoServiceProvider())
        //        {
        //            PasswordDeriveBytes pdb = new PasswordDeriveBytes("ENTERAKEYHERE", new byte[0]);
        //            cypher.Key = pdb.GetBytes(16);
        //            cypher.IV = pdb.GetBytes(8);

        //            using (MemoryStream ms = new MemoryStream())
        //            {
        //                using (CryptoStream cs = new CryptoStream(ms, cypher.CreateDecryptor(), CryptoStreamMode.Write))
        //                {
        //                    byte[] data = Convert.FromBase64String(encryptedString);
        //                    cs.Write(data, 0, data.Length);
        //                    cs.Close();

        //                    return Encoding.Unicode.GetString(ms.ToArray());
        //                }
        //            }
        //        }
        //    }
        //    catch
        //    {
        //        return String.Empty;
        //    }
        //}

        ///// <summary>
        ///// Encrypts a string
        ///// </summary>
        ///// <param name="decryptedString"
        ///// <returns></returns>
        //public static String EncryptString(string decryptedString)
        //{
        //    if (String.IsNullOrEmpty(decryptedString)) return String.Empty;

        //    using (TripleDESCryptoServiceProvider cypher = new TripleDESCryptoServiceProvider())
        //    {
        //        PasswordDeriveBytes pdb = new PasswordDeriveBytes("ENTERAKEYHERE", new byte[0]);

        //        cypher.Key = pdb.GetBytes(16);
        //        cypher.IV = pdb.GetBytes(8);

        //        using (MemoryStream ms = new MemoryStream())
        //        {
        //            using (CryptoStream cs = new CryptoStream(ms, cypher.CreateEncryptor(), CryptoStreamMode.Write))
        //            {
        //                byte[] data = Encoding.Unicode.GetBytes(decryptedString);

        //                cs.Write(data, 0, data.Length);
        //                cs.Close();

        //                return Convert.ToBase64String(ms.ToArray());
        //            }
        //        }
        //    }
        //}

        ///// <summary>
        ///// Decrypts a given value as type of T, if unsuccessful the defaultValue is used
        ///// </summary>
        ///// <typeparam name="T"></typeparam>
        ///// <param name="value"></param>
        ///// <param name="defaultValue"></param>
        ///// <returns></returns>
        //public T DecryptObject<T>(object value, T defaultValue)
        //{
        //    if (value == null) return defaultValue;

        //    try
        //    {
        //    Type conversionType = typeof(T);

        //    // Some trickery for Nullable Types
        //    if (conversionType.IsGenericType && conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
        //    {
        //        conversionType = new NullableConverter(conversionType).UnderlyingType;
        //    }

        //    return (T)Convert.ChangeType(DecryptString(Convert.ToString(value)), conversionType);
        //    }
        //    catch 
        //    {
        //        // Do nothing
        //    }

        //    return defaultValue;
        //}
    }
}